package cn.itsource.pethome.system.pet.controller;

import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.system.pet.service.IPetService;
import cn.itsource.pethome.system.pet.domain.Pet;
import cn.itsource.pethome.system.pet.query.PetQuery;
import cn.itsource.pethome.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;


    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet, HttpServletRequest request){
        try {
            if( pet.getId()!=null)
                petService.update(pet);
            else
                petService.add(pet,request);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id")Long id)
    {
        return petService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Pet> list(){

        return petService.findAll();
    }


    /**
    * 分页查询数据
    * 需求：如果 分角色 系统级别查看全部  如果是店铺级别 只能看自己店铺的(我们只做这个)   如果是店员 只能看自己的
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody PetQuery query,HttpServletRequest request)
    {
        try {
            List<Pet> byQuery = petService.findByQuery(query,request);
            Long count = petService.findByQueryCount(query,request);
            return AjaxResult.success().setData(byQuery).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    /**
     * 这个接口只能查询上架的物品
     * @param query
     * @return
     */
    @PostMapping("/pagelist")
    public AjaxResult json(@RequestBody PetQuery query)
    {
        try {
            query.setState(BaseConstants.Pet.STATE_UP);
            List<Pet> byQuery = petService.findByQuery(query);
            Long count = petService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    /**
     * 批量上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            String errorMsg = petService.onsale(ids);
            return AjaxResult.success().setMsg(errorMsg);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    /**
     * 批量下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
}
